var app = getApp();
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var app = getApp();

Page({
  data: {
    title: "",
    bannerList: [],
    goodsModuleList: [],
    circular: true,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    userInfo: {},
    dataLoaded: false
  },

  onShareAppMessage: function () {
    return {
      title: this.data.shop.shareTitle,
      path: "pages/index/index"
    }
  },

  _initApp(cb) {
    wx.getExtConfig({
      success: function (res) {
        var {host, alias} = res.extConfig;

        if (host) {
          app.setHost(host);
        }
        if (alias) {
          app.setAlias(alias);
        }
        cb && cb();
      }
    });
  },

  _toMap() {
    var shop = this.data.shop;

    ajax.G('/openapi/CAddress/getLocation', {
      addr: shop.address
    }, function (data) {
      var addressInfo = JSON.parse(data.result)['result'];

      wx.openLocation({
        latitude: addressInfo.location.lat,
        longitude: addressInfo.location.lng,
        name: shop.name,
        address: shop.address,
      })
    })
  },

  onLoad: function () {
    var _self = this;

    _self._initApp(function () {
      _self.getNormal();
      _self._quietLogin();
    });
  },
  getNormal: function () {
    var _self = this;

    ajax.P('/openapi/CHome/index', {}, function (result) {
      var d = result.result;

      _self.setData({
        shop: {
          name: d.title,
          phone: d.contact,
          weixin: d.weixin,
          qq: d.qq,
          address: d.address,
          time: d.hours,
          logo: d.logo,
          shareTitle: d.share_title,
          kfState: d.kf_state
        },
        bannerList: d.bannerList,
        goodsModuleList: d.goodsModuleList,
        dataLoaded: true,
      });

      app.setShop(_self.data.shop);
    })
  },

  /**
    * openid静默登陆
    */
  _quietLogin: function () {
    var $this = this;

    wx.login({
      success: function (res) {
        ajax.G(
          '/openapi/CHome/getUserOpenID',
          {
            jscode: res.code
          },
          function (d) {
            app.setOpenId(d.result.openid);
            $this._openIdLogin(d.result.openid);
          }
        );
      },
      fail: function () { },
      complete: function () { }
    })
  },

  /**
   * openid静默登陆
   */
  _openIdLogin: function (openid) {
    var $this = this;
    ajax.P(
      '/openapi/CHome/login',
      {
        openId: openid
      },
      function (d) {
        var userSid = d.result.userSid;
        if (userSid) {
          app.setUserSid(userSid);
        }
      }
    );
  },
  /**
    * 调用电话接口
    */
  doTel: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.shop.phone
    })
  },

  _copyText(evt) {
    console.log(evt);
    var {copyVal, copySuccess} = evt.currentTarget.dataset;

    wx.setClipboardData({
      data: copyVal,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            if (copySuccess) {
              util.showOK(copySuccess || '复制成功');
            }
          }
        })
      }
    })
  }
})  